package cn.seaboot.admin.activemq;

import org.fusesource.hawtbuf.AsciiBuffer;
import org.fusesource.hawtbuf.UTF8Buffer;
import org.fusesource.mqtt.client.FutureConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;

/**
 * 发生 MQTT 消息
 *
 * @author Mr.css
 * @version 2024-10-15 9:16
 */
public class MQTTSender {

    public static void main(String[] args) throws Exception {
        MQTT mqtt = new MQTT();
//        mqtt.setHost("localhost", 1883);
        mqtt.setHost("47.122.57.10", 1883);
        mqtt.setUserName("admin");
        mqtt.setPassword("maker");

        FutureConnection connection = mqtt.futureConnection();
        connection.connect().await();

        UTF8Buffer topic = new UTF8Buffer("mqtt");

        int messageAmount = 1;
        while (messageAmount --> 0) {
            connection.publish(topic, new AsciiBuffer("test message" + messageAmount), QoS.EXACTLY_ONCE, true);
            System.out.println("send message " + messageAmount);
        }

        connection.disconnect().await();
    }
}
